<!-- 登陆页面 -->
<template>
  <div class="Login">
    <!-- 顶部 -->
    <van-nav-bar title="登录">
      <template #left>
        <van-icon name="arrow-left" color="#fff" @click="goBack">返回</van-icon>
      </template>
      <template #right>
        <router-link to="/register">注册</router-link>
      </template>
    </van-nav-bar>

    <!-- 通知栏 -->
    <van-notice-bar left-icon="volume-o" :text="msg" />

    <!-- 头像 -->
    <van-image class="login_img" width="100px" height="100px" :src="require('@/assets/avatar/00fb2f5b2c9a39a8012034f76e8c48.jpg')"/>

    <!-- 标签栏 -->
    <van-tabs type="card" color="#1989fa" animated swipeable @click="onClick">
      <!-- 用户名表单 -->
      <van-tab title="用户名" style="margin-top: 1.5vh">
        <van-form @submit="onSubmit">
          <!-- 用户名 -->
          <van-field v-model="username" name="用户名" label="用户名" placeholder="请输入用户名"
          :rules="[{ required: true },{ pattern: /^\w{6,15}$/, message: '用户名格式错误！' }]" />
          <!-- 密码 -->
          <van-field v-model="password" type="password" name="密码" label="密码" placeholder="请输入密码"
          :rules="[{ required: true },{ pattern: /^\d{6}$/, message: '密码格式错误！' }]"/>
          <!-- 登录按钮 -->
          <div style="margin: 2.4vh">
            <van-button round block type="info" native-type="submit">用户名登录</van-button>
          </div>
        </van-form>
      </van-tab>

      <!-- 手机号表单 -->
      <van-tab title="手机号码" style="margin-top: 1.5vh">
        <van-form @submit="onSubmit2">
          <!-- 手机号 -->
          <van-field v-model="phone" name="手机号码" label="手机号码" placeholder="请输入手机号码" 
          :rules="[{ required: true },{ pattern: /^1[3-9]\d{9}$/, message: '手机号码格式错误！' }]"/>
          <!-- 密码 -->
          <van-field v-model="password" type="password" name="密码" label="密码" placeholder="请输入密码"
          :rules="[{ required: true },{ pattern: /^\d{6}$/, message: '密码格式错误！' }]"/>
          <!-- 登录按钮 -->
          <div style="margin: 2.4vh">
            <van-button round block type="info" native-type="submit">手机号码登录</van-button>
          </div>
        </van-form>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { axios_hitokoto2,axios_login1,axios_login2 } from "@/api/data_axios";
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "",
      phone: "",
      password: "",
      msg: "",
    };
  },
  mounted() {
    this.notice();
  },
  methods: {
    // 通知栏文字
    notice() {
      let data = {
        c:'d',
        encode:'text'
      }
      axios_hitokoto2(data).then(res=>{
        this.msg = res;
      })
    },
    // 点击用户名登录之后执行，验证表单
    onSubmit() {
      let data = {
        username:this.username,
        password:this.password
      }
      axios_login1(data).then(res=>{
        if (res.code == 200) {
          let that = this;
          Toast({
            message: "登录成功",
            duration: 1000,
            onClose: function () {
              // 接收数据库返回的信息
              that.$store.commit("loninOK", [
                res.result.username,
                res.result.nickname,
                res.result.avatar,
                res.result.phone,
              ]);
              that.$router.push("/home");
            },
          });
        } else {
          Toast.fail("登录失败");
        }
      })
    },
    // 点击手机号码登录之后执行，验证表单
    onSubmit2() {
      let data = {
        phone:this.phone,
        password:this.password
      }
      axios_login2(data).then(res=>{
        if (res.code == 200) {
          let that = this;
          Toast({
            message: "登录成功",
            duration: 1000,
            onClose: function () {
              // 接收数据库返回的信息
              that.$store.commit("loninOK", [
                res.result.username,
                res.result.nickname,
                res.result.avatar,
                res.result.phone,
              ]);
              that.$router.push("/home");
            },
          });
        } else {
          Toast.fail("登录失败");
        }
      })
    },
    // 标签页清空
    onClick(_, title) {
      if (title == "用户名") {
        this.username = ""; 
        this.password = "";
      } else {
        this.phone = "";
        this.password = "";
      }
    },
    // 回退一步
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
.Login {
  text-align: center;
  .van-nav-bar {
    background-color: $base-color;
    ::v-deep .van-nav-bar__title{
      font-weight: bold;
    }
    .van-icon{
      font-weight: bold;
    }
  }
  .van-nav-bar__right a {
    color: white;
    font-weight: bold;
  }
  .login_img {
    margin-top: 4.5vh;
    margin-bottom: 4.5vh;
  }
}
</style>
